﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using socialnexus.classes.pages;
using socialnexus.classes.models;

namespace socialnexus
{
    public partial class ViewMessage : AuthenticatedUserPage
    {
        protected string VideoURL = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUser su = GetUser();
            SiteUser friend = null;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();
            string queryString = "SELECT TOP 1 * FROM Messages " +
                "WHERE MessageId=@MESSAGEID and ToUserId=@TOUSERID";

            string MessageId = Request.QueryString["MessageId"].ToString();

            SqlCommand cmd = new SqlCommand(queryString, con);
            cmd.Parameters.Add(new SqlParameter("@MESSAGEID", MessageId));
            cmd.Parameters.Add(new SqlParameter("@TOUSERID", su.SiteUserId));

            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                VideoURL = reader["VideoURL"].ToString();
                string friendId = reader["FromUserId"].ToString();
                reader.Close();

                friend = SiteUser.GetById(friendId);
                imgFriend.ImageUrl = friend.PictureURL;
                lblFriendName.Text = friend.FirstName + " " + friend.LastName;

                cmd = new SqlCommand("UPDATE Messages SET IsRead = 1 WHERE MessageID = @ID", con);
                cmd.Parameters.Add(new SqlParameter("@ID", MessageId));
                cmd.ExecuteNonQuery();
            }
            else
            {
                Response.Redirect("~/Inbox.aspx");
            }

            con.Close();
        }
    }
}
